//
//  BIDManageViewController.m
//  EducationAssistant
//
//  Created by mal on 15/6/2.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import <PgyUpdate/PgyUpdateManager.h>
#import "BIDManageViewController.h"
#import "BIDGlobalMethods.h"
#import "BIDLoginViewController.h"
//#import "UserProfileManager.h"

#import "BIDHomeViewController.h"
#import "BIDLifeViewController.h"
#import "BIDCommunityDedicationViewController.h"
#import "BIDPersonalInfoViewController.h"

#import "BPush.h"


static NSString *userInfoURL = @"userManage/getUserinfo.action";
//两次提示的默认间隔
static const CGFloat kDefaultPlaySoundInterval = 3.0;
static NSString *kMessageType = @"MessageType";
static NSString *kConversationChatter = @"ConversationChatter";

/**
 *  获取当前用户当前考勤设置项
 */
static NSString *getCurAttendanceSettingsURL = @"signScheduleAndroid/getCurrentValid.action";

//绑定推送用到的channelID
static NSString *bindChannelIDURL = @"pushAndroid/add.action";

@interface BIDManageViewController ()<UITabBarDelegate>
{
    IBOutlet UITabBar *_tabBar;
    NSInteger _selectedIndex;
    UIViewController *_curVC;
    BOOL _bShowArcMenu;
    BIDHomeViewController *_homeVC;
    BIDLifeViewController *_lifeVC;
    BIDCommunityDedicationViewController *_communityDedicationVC;
    BIDPersonalInfoViewController *_personalVC;
}

@property (strong, nonatomic) NSDate *lastPlaySoundDate;

@end

@implementation BIDManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[PgyUpdateManager sharedPgyManager] checkUpdate];
    // Do any additional setup after loading the view from its nib.
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    _selectedIndex = 0;
    [self toBindChannelID];
    [self prepareForLayout];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)toBindChannelID
{
    //百度推送
    [BPush bindChannelWithCompleteHandler:^(id result, NSError *error) {
        // 需要在绑定成功后进行 settag listtag deletetag unbind 操作否则会失败
        if (result)
        {
            NSString *strChannelID = [BPush getChannelId];
            NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], bindChannelIDURL];
            NSString *strPost = [[NSString alloc] initWithFormat:@"push.memberId=%@&push.channelId=%@&push.deviceType=%@", [AppDelegate getUserId], strChannelID, @4];
            //
            BIDDataCommunication *dc = [[BIDDataCommunication alloc] init];
            [dc getDataFromNet:strRequestURL postValue:strPost completionHandler:^(id obj, NSError *err){
                if(obj && !err)
                {
                    if([obj isKindOfClass:[NSDictionary class]])
                    {
                        NSDictionary *dic = (NSDictionary*)obj;
                        NSNumber *value = dic[@"head"][@"success"];
                        if([value intValue] == 1)
                        {
                        }
                    }
                }
                else
                {}
            }];
        }
    }];
}

- (void)prepareForLayout
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBarHidden = NO;
    //创建tabBar
    NSArray *dataSourceArr = @[@{@"title":@"首页", @"normal":@"home.png", @"select":@"home_select.png"},
                               @{@"title":@"生活", @"normal":@"livelihood.png", @"select":@"livelihood_select.png"},
                               @{@"title":@"社区公益", @"normal":@"love.png", @"select":@"love_select.png"},
                               @{@"title":@"我", @"normal":@"me.png", @"select":@"me_select.png"}];
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    //
    CGRect tabBarFrame = CGRectMake(0, screenSize.height - 64 - 49, screenSize.width, 49.f);
    _tabBar = [[UITabBar alloc] initWithFrame:tabBarFrame];
    _tabBar.delegate = self;
    _tabBar.frame = tabBarFrame;
    [self createTabBarWithArray:dataSourceArr];
    [self.view addSubview:_tabBar];
    //
    _homeVC = [[BIDHomeViewController alloc] initWithNibName:@"BIDHomeViewController" bundle:nil];
    [self displayContentController:_homeVC];
}

/**
 创建TabBar

 @param arr <#arr description#>
 */
- (void)createTabBarWithArray:(NSArray *)arr
{
    NSMutableArray *tabBarItemArr = [NSMutableArray array];
    int i=0;
    for(NSDictionary *dic in arr)
    {
        UIImage *normalImg = [UIImage imageNamed:dic[@"normal"]];
        UIImage *selectImg = [UIImage imageNamed:dic[@"select"]];
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:dic[@"title"] image:normalImg selectedImage:selectImg];
        item.tag = i;
        [tabBarItemArr addObject:item];
        i++;
    }
    [_tabBar setItems:tabBarItemArr];
    [_tabBar setSelectedItem:tabBarItemArr[0]];
    [_tabBar setTintColor:RGBACOLOR(27, 211, 205, 1.f)];
}

- (void) displayContentController: (UIViewController*) content
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
    frame.size.width = screenSize.width;
    if([content isKindOfClass:[BIDHomeViewController class]])
    {
        frame.size.height = screenSize.height - 49;
        CGRect tabBarFrame = CGRectMake(0, screenSize.height - 49, screenSize.width, 49.f);
        _tabBar.frame = tabBarFrame;
    }
    else
    {
        frame.size.height = screenSize.height - 64 - 49;
        CGRect tabBarFrame = CGRectMake(0, screenSize.height - 64 - 49, screenSize.width, 49.f);
        _tabBar.frame = tabBarFrame;
    }
    content.view.frame = frame;
    [self addChildViewController:content];
    [self.view addSubview:content.view];
    [content didMoveToParentViewController:self];
    _curVC = content;
}

- (void) hideContentController: (UIViewController*) content
{
    [content willMoveToParentViewController:nil];
    [content.view removeFromSuperview];
    [content removeFromParentViewController];
}

- (void)showTabBar:(BOOL)bShow
{
    if(bShow)
    {
        _tabBar.hidden = NO;
    }
    else
    {
        _tabBar.hidden = YES;
    }
}

#pragma mark -UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    NSInteger curIndex = item.tag;
    if(curIndex != _selectedIndex)
    {
        self.navigationController.navigationBarHidden = NO;
        _selectedIndex = curIndex;
        if(_curVC)
        {
            [self hideContentController:_curVC];
        }
        switch(curIndex)
        {
            case 0:
            {
                //首页
                if(!_homeVC)
                {
                    _homeVC = [[BIDHomeViewController alloc] initWithNibName:@"BIDHomeViewController" bundle:nil];
                }
                [self displayContentController:_homeVC];
            }
                break;
            case 1:
            {
                //生活
                if(!_lifeVC)
                {
                    _lifeVC = [[BIDLifeViewController alloc] init];
                }
                [self displayContentController:_lifeVC];
                CGRect frame = _lifeVC.myTableView.frame;
                frame.size.height = [UIScreen mainScreen].bounds.size.height-49-64;
                _lifeVC.myTableView.frame = frame;
            }
                break;
            case 2:
            {
                //社区公益
                if(!_communityDedicationVC)
                {
                    _communityDedicationVC = [[BIDCommunityDedicationViewController alloc] init];
                }
                [self displayContentController:_communityDedicationVC];
                CGRect frame = _communityDedicationVC.myTableView.frame;
                frame.size.height = [UIScreen mainScreen].bounds.size.height-49-64;
                _communityDedicationVC.myTableView.frame = frame;
            }
                break;
            case 3:
            {
                //我的
                if(!_personalVC)
                {
                    _personalVC = [[BIDPersonalInfoViewController alloc] initWithNibName:@"BIDPersonalInfoViewController" bundle:nil];
                }
                [self displayContentController:_personalVC];
            }
                break;
        }
    }
}



@end
